﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
     layui.use(['jquery', 'form', 'laydate', 'tableEdit', 'commonTable', 'table', 'common', 'optimizeSelectOption','upload'], function () {
         var form = layui.form,
             $ = layui.$,
             laydate = layui.laydate,
             tableEdit = layui.tableEdit,
             table = layui.table,
             commonTable = layui.commonTable,
             common = layui.common;
              upload = layui.upload;
         var keyValue = $.request("keyValue");
           var params = [];
          var listData = [];
         //权限字段
         common.authorizeFields('adminform');

         //此处需修改
         //类型为时间时
          laydate.render({
             elem: '#F_HeadDate'
             ,type:'month'
             , trigger: 'click',
             format: 'yyyy-MM',
         });

         $(function () {
             initControl();
             if (!!keyValue) {
                  common.ajax({
                     url: '/SalaryManagement/Yearendbonus/GetFormJson',
                    dataType: 'json',
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                         if (!!data.list && data.list.length > 0) {
                             listData = data.list;
                         }
                         var now=new Date(data.F_HeadDate);
                          $('#F_HeadDate').val(now.getFullYear() + "-" +((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1));

                     }
                });
             } else {
                 var code = new Date().formatDate('yyyyMMddhhmmss');
                 $('#F_EnCode').val(code);
                 var now = new Date();
                   $('#F_HeadDate').val(now.getFullYear() + "-" +((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1));
             }

            form.render();

             var cols = [[
                 { type: "checkbox", width: 50 }, 
                 {
                     field: 'F_DateTime', title: '日期', minWidth: 160, sort: true, event: 'Datetime', config: { type: 'date', dateType: 'date' }
                     , templet: function (d) {
                         var dd= new Date(d.F_DateTime);
                            return dd.getFullYear()+'-'+(dd.getMonth()+1)+'-'+dd.getDate();
                     }
                 },
                 { field: 'F_UserId', title: 'id', minWidth: 145, sort: true,hide:true,config: { type: 'signedInput' }  },

                 {
                       field: 'F_UserNum', title: '工号', minWidth: 145, sort: true, config: { type: 'signedInput' }

                 },
                  { field: 'F_RealName', title: '姓名', minWidth: 145, sort: true, config: { type: 'signedInput' }  },

                

                     { field: 'F_Amount', title: '奖金', width: 80, event: 'Amount', config: { type: 'signedInput' } },



             ]];
             commonTable.rendertable({
                 elem: '#currentTableId',
                 data: listData,
                 defaultToolbar: [],
                 search: false,
                 limit: 9999,//每页数据 默认
                 page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                     layout: ['count'] //自定义分页布局
                     , first: false //不显示首页
                     , last: false //不显示尾页
                 },
                 height: 'full-185',
                 filter: {
                     clearFilter: false,
                 },
                 cols: cols
             });

             var aopTable = tableEdit.aopObj(cols); //获取一个aop对象
             aopTable.on('tool(currentTableFilter)', function (obj) {
                 var field = obj.field; //单元格字段
                 var value = obj.value; //修改后的值
                 var data = obj.data; //当前行旧数据
                 var event = obj.event; //当前单元格事件属性值
                 var dataCache = obj.dataCache;// 得到当前行缓存数据
                 var tr = obj.tr; // 得到当前行 <tr> 元素的 jQuery 对象

                  var update = {};
                   // if (event === 'Shift') { //查看
                  
                   //  update['F_Shift'] = value.value;
                    

                   // }


               

                 update[field] = value;

                 //把value更新到行中
                 obj.update(update);
             });
             //行点击事件监听，控制按钮显示
             var oneList = [];//选择1条显示
             var moreList = ["NF-delete"];//多条显示
             commonTable.tableRowClick("checkbox", "currentTableFilter", "currentTableId", oneList, moreList);
        });
        //普通图片上
         var dataJson = [];
        var index;
        var uploadInst = upload.render({
            elem: '#import'
            , url: "/SalaryManagement/Yearendbonus/CheckFile"//改成您自己的上传接口
            , size: 1024
            , accept: 'file'
            , headers: { 'X-CSRF-TOKEN': "" }
            , before: function (obj) {
                if ($('[name=__RequestVerificationToken]').length > 0) {
                    // 1、动态添加headers中的参数
                    this.headers['X-CSRF-TOKEN'] = $('[name=__RequestVerificationToken]').val();
                }
                // 2、动态添加的其它参数
                //this.data.type = outerType
                index = parent.layer.load(0, {
                    shade: [0.5, '#000'], //0.1透明度的背景
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    //失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                    common.modalMsg(res.msg, "warning");
                    parent.layer.close(index);
                    return false;
                }
                dataJson = res.data;
                //执行重载
                var alldata = table.cache.currentTableId;
             var data=alldata.concat(dataJson);
            table.reload('currentTableId', { data: data });
                // table.reload('currentTableId', {
                //     data: dataJson
                // }, 'data');
                parent.layer.close(index);
            }
            , error: function () {
                //演示失败状态，并实现重传
                parent.layer.close(index);
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        wcLoading.close();
         table.on('toolbar(currentTableFilter)', function (obj) {
             var data = table.checkStatus('currentTableId').data;
             // if (obj.event === 'add') {  // 监听添加操作\
             //     var alldata = table.cache.currentTableId;
             //     alldata.push({ F_Id: uuid(), F_Datetime: new Date().formatDate('yyyy-MM-dd'), F_UserNum: '', F_RealName: '',  F_Times1: '0', F_Times2: '0', F_Times3: '0', F_Times4: '0', F_SubsidyAmount: '0' })
             //     table.reload('currentTableId', { data: alldata })
             //  }else
              
              if(obj.event === 'add') {
                  var alldata = table.cache.currentTableId;
                  var ids=[];

             for(var i=0;i<alldata.length;i++){
                 ids.push(alldata[i].F_UserId);


             }
            common.modalOpen({
                    title: "选择人员",
                     url: "/SystemOrganize/User/AddForm2?ids="+ids.join(','),
                    width: "80%",
                    height: "80%",
                });

            }

             else if (obj.event === 'delete') {
                 if (data.length == 0) {
                     common.modalMsg("未选中数据", "warning");
                     return false;
                 }
                 common.modalConfirm("注：您确定要删除选择的数据吗？", function (r) {
                     if (r) {
                         var alldata = table.cache.currentTableId;
                         for (var j = 0; j < data.length; j++) {
                             for (var i = alldata.length - 1; i >= 0; i--) {
                                 if (alldata[i].F_Id == data[j].F_Id) {
                                     alldata.splice(i, 1);
                                     break;
                                 }
                             }
                         }
                         table.reload('currentTableId', { data: alldata })
                     }
                 });
             }
             return false;
         });
        function initControl() {
            //此处需修改
            //绑定数据源
            //类型为下拉框时
        

        }

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = data.field;
             postData.list = table.cache.currentTableId;
             if (!postData.list || postData.list.length == 0) {
                 common.modalMsg("明细不能为空!");
                 return false;

             }
           
             if (!postData["F_EnabledMark"]) postData["F_EnabledMark"] = false;
       

            common.submitForm({
                 url: '/SalaryManagement/Yearendbonus/SubmitForm?keyValue=' + keyValue,
                param: postData,
                success: function () {
                    common.parentreload('data-search-btn');
                }
            })
            return false;
        });

         function settable(all){


           var alldata = table.cache.currentTableId;


           var data=alldata.concat(all);
         table.reload('currentTableId', { data: data });

         }
         window.settable=function(data){

             settable(data);
         }
    });


</script>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">


                <div class="layui-form-item">
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label required">编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_EnCode" name="F_EnCode" autocomplete="off" lay-verify="required" placeholder="请输入" lay-filter="a" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label required">名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_FullName" name="F_FullName" autocomplete="off" lay-verify="required" placeholder="请输入" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label required">日期</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_HeadDate" name="F_HeadDate" autocomplete="off" lay-verify="required" placeholder="请输入" lay-filter="a" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label ">备注</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_Description" name="F_Description" autocomplete="off" placeholder="请输入" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item ">
                    <div class="layui-col-md12 layui-col-sm12 ">
                        <form class="layui-form" action="">
                            <button type="submit" class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="download"> <i class="layui-icon">&#xe601;</i>模板下载</button>
                            <button type="button" id="import" class="layui-btn layui-btn-sm layui-btn-primary"><i class="layui-icon">&#xe67c;</i>上传Excel</button>
                        </form>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md12 layui-col-sm12">


                        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container" id="toolbar">
                  
                     <button id="NF-add" name="NF-add" class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe642;</i>添加人员</button>

                    <button id="NF-delete" name="NF-delete" class="layui-btn layui-btn-sm layui-btn-danger layui-hide" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>
                </div>
            </script>
        </div>
    </div>
</body>

